package com.polaris.live.popup

import android.content.Context
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.PopupBuySuccessBinding
import com.polaris.live.resp.back_resp.PayerShopResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.image.ImageUtils

/**
 * 购买商品成功弹窗
 *
 * @author Created by 天晴 on 2024/7/25 9:43
 * @since 1.0.0
 */
class BuySuccessPopup(
    context: Context,
    val payerShopResp: PayerShopResp,
    val days: Int,
    val otherUserId: Long? = null,
    val look: (() -> Unit)? = null,
    val wear: (() -> Unit)? = null,
) : CenterPopupView(context) {

    private lateinit var mBinding: PopupBuySuccessBinding

    override fun getImplLayoutId(): Int {
        return R.layout.popup_buy_success
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupBuySuccessBinding.bind(popupImplView)
        mBinding.baseLl.setOnSingleClickListener {
            dismiss()
        }
        mBinding.apply {
            Glide.with(context)
                .load(ImageUtils.imgPath(payerShopResp.displayUrl))
                .into(commodityImg)
            nameTv.text = "${payerShopResp.shopName}*$days${StringUtils.getString(string.one_days)}"
            if (otherUserId == null) {
                tipsTv.text = StringUtils.getString(string.gift_to_me)
                lookLl.visibility = VISIBLE
            } else {
                tipsTv.text = StringUtils.getString(string.gift_to_other)
                titleTv.text = StringUtils.getString(string.gift_successful)
                ok.visibility = VISIBLE
            }
        }
        mBinding.ok.setOnSingleClickListener {
            dismiss()
        }
        mBinding.look.setOnSingleClickListener {
            look?.invoke()
            dismiss()
        }
        mBinding.wear.setOnSingleClickListener {
            wear?.invoke()
            dismiss()
        }

    }

    override fun getMaxHeight(): Int {
        return ScreenUtils.getScreenHeight()
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()

    }

    override fun getPopupHeight(): Int {
        return ScreenUtils.getScreenHeight()
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()

    }

    companion object {
        fun show(
            context: Context, payerShopResp: PayerShopResp,
            days: Int,
            otherUserId: Long? = null,
            onShow: ((show: Boolean) -> Unit)? = null,
            look: (() -> Unit)? = null,
            wear: (() -> Unit)? = null,
        ) {
            XPopup.Builder(context)
                .isViewMode(false)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .hasShadowBg(false)
                .setPopupCallback(object : SimpleCallback() {
                    override fun onShow(popupView: BasePopupView?) {
                        super.onShow(popupView)
                        onShow?.invoke(true)
                    }

                    override fun onDismiss(popupView: BasePopupView?) {
                        super.onDismiss(popupView)
                        onShow?.invoke(false)

                    }
                })
                .asCustom(BuySuccessPopup(context, payerShopResp, days, otherUserId, look, wear))
                .show()
        }
    }
}